package com.douguo.recipe.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;

import com.douguo.recipe.HomeActivity;
import com.douguo.recipe.LoadingActivity;
import com.douguo.recipe.RecipelistActivity;
import com.douguo.recipe.object.Caipin;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Caixi;
import com.douguo.recipe.object.HomeCategory;
import com.douguo.recipe.object.HomeRecipe;
import com.douguo.recipe.object.Kouwei;
import com.douguo.recipe.object.Sort;
import com.douguo.recipe.object.Tese;

public class GetHomeCategories {
	private static final String TAG = "GetHomeCaixi";

	public static void getHomeCategories(final Handler handler) {
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		
		NameValuePair nvpCount = new BasicNameValuePair("action", "cate");
		nvps.add(nvpCount);
		final String url = Constant.URL_GET_HOME;
		
		
		new Thread(new Runnable() {

			public void run() {
				DouguoConnection mConn = new DouguoConnection(url, nvps,
						mParseListener, DouguoConnection.GET_METHOD);
				List<HomeCategory> categories = (List<HomeCategory>) mConn.Connect();
				handler.sendMessage(Message.obtain(handler,
						Constant.MSG_HOT_CATEGORIES, categories));

			}

		}).start();
	}

	private static OnParseListener mParseListener = new OnParseListener() {

		public Object onParsing(InputStream is) {
			List<HomeCategory> categories = null;
			XmlPullParser parser = Xml.newPullParser();
			HomeCategory currentHomeCategory = null;
			Sort currentSort = null;
			boolean done = false;

			try {

				parser.setInput(is, null);
				int eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT && !done) {
					String name = null;
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						categories = new ArrayList<HomeCategory>();
						break;
					case XmlPullParser.START_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("data")) {
							currentHomeCategory = new HomeCategory();
							String type = parser.getAttributeValue(null, "cate");
							if(type.equalsIgnoreCase("caixi")) {
								currentSort = new Caixi();
							}
							else if(type.equalsIgnoreCase("tese")) {
								currentSort = new Tese();
							}
							else if(type.equalsIgnoreCase("caipin")) {
								currentSort = new Caipin();
							}
							else if(type.equalsIgnoreCase("kouwei")) {
								currentSort = new Kouwei();
							}
							currentHomeCategory.sort = currentSort;
//							currentSort = new Sort();
							
							
							
						} else if (currentHomeCategory != null) {
							if (name.equalsIgnoreCase("icon")) {
								String icon = parser.nextText();
								icon = icon.substring(0, icon.lastIndexOf(".png"));
								currentHomeCategory.icon = icon;
							} else if (name.equalsIgnoreCase("id") && currentHomeCategory.sort != null) {
								currentHomeCategory.sort.id = parser.nextText();
							} else if (name.equalsIgnoreCase("title")) {
								currentHomeCategory.title = parser.nextText();
							} 

						}
						break;
					case XmlPullParser.END_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("data")
								&& currentHomeCategory != null) {
							Log.d(TAG, currentHomeCategory.title);
							Log.d(TAG, currentHomeCategory.icon);
							categories.add(currentHomeCategory);
						}
						break;
					case XmlPullParser.END_DOCUMENT:
						done = true;
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return categories;
		}

	};

}
